﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using XinBang.BLL;
using XinBang.Entity;
using XinBang.Util;

namespace XinBang.Web.Admin
{
    public partial class User : System.Web.UI.Page
    {
        private MemberBL mbl = new MemberBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Request.QueryString["action"];
            switch (action)
            {
                case "login":
                    login();
                    break;
                case "logout":
                    logout();
                    break;
                case "destroy":
                    destroy();
                    break;
                case "register":
                    register();
                    break;
                default:
                    login();
                    break;
            }
        }
        private void register()
        {
            string username = Request.Form["username"];
            string password = Request.Form["password"];
            string email = Request.Form["email"];
            bool suc = false;
            if (mbl.IsExsitByUsername(username))
            {
                Response.Write("{success: '" + suc + "',describe:'该用户名已经存在。'}");
            }
            else {
                if (mbl.IsExsitByEmail(email))
                {
                    Response.Write("{success: '" + suc + "',describe:'该邮箱已经存在。'}");
                }
                else {
                    Member m = new Member();
                    m.Username = username;
                    m.Email = email;
                    m.Password = StringSecurity.StringToSHA1Hash(password);
                    mbl.InsertMember(m);
                    SessionUtil.SaveMemberSession(m);
                    //Power p = new Power();
                    //p.Memid = mbl.GetMemberByEmail(email);
                    //p.Rolid = RoleBL.GetRoleByRoleName(RoleLevel.STUDENT);
                    //PowerBL.InsertPower(p);
                    suc = true;
                    Response.Write("{success: '" + suc + "',describe:'注册成功，现在转入后台。'}");
                }
            } 
            Response.End();
        }
        protected void destroy()
        {
            SessionUtil.RemoveMemberSession();
            Response.Redirect("./Index.aspx");
        }
        protected void login()
        {
            bool suc = false;
            string desc = "";
            string url = "";
            string username = Request.Form["username"];
            string password = Request.Form["password"];
            string email = Request.Form["email"];
            Member m = mbl.GetMemberByEmail(email);
            if (m!=null)
            {
                if (m.Password == StringSecurity.StringToSHA1Hash(password))
                {
                    SessionUtil.SaveMemberSession(m);
                    //if (PowerBL.GetRoleLevelByMember(m) > RoleBL.GetRoleLevelByRoleName(RoleLevel.TEACHER))
                    //{
                    //    url = "../platform/index.html";
                    //    url = "../admin/index.html";
                    //}
                    //else
                    {
                        url = "../admin/index.html";
                    }
                    suc = true;
                    desc = "登录成功！转入后台！";
                }
                else
                {
                    desc = "密码不对！";
                }
            }
            else {
                desc = "不存在的邮箱地址！";
            }
            Response.Write("{success: '" + suc+"',redirect:'"+ url +"',describe:'"+ desc +"'}");
            Response.End();
        }
        protected void logout()
        {
            SessionUtil.RemoveMemberSession();
            Response.Redirect("~/");

        }
    }
}